# -*- coding: utf-8 -*-
# @Time : 2022/6/27 下午2:36
# @Author : wyf
# @FileName: newrank.py
# @Email: yunfei.wang@innvote.com
import execjs
import requests
from lxml import etree
from pymongo import MongoClient


def js_from_file(file_name):
    """
    读取js文件
    :return:
    """
    with open(file_name, 'r', encoding='UTF-8') as file:
        result = file.read()
    return result


def get_params():
    context = execjs.compile(js_from_file('./newrank.js'))
    params = context.call("get_params")
    return params


def xin_shi_get_data(_type, start=1, size=50, rank_date="2022-06-26", interval="day"):
    """
    获取 新视(视频号数据)
    :return:
    """
    url = "https://www.newrank.cn/xdnphb/nr/xinshi/video/getMainIndexRank"
    _body = {
        "interval": interval,
        "rank_date": rank_date,
        "type": _type,
        "start": start,
        "size": size
    }
    req = requests.post(url, json=_body, params=get_params())
    print(req.json())
    if req.status_code == 200 and req.json().get("data"):
        xs_list = req.json().get("data").get("list")
        for xs in xs_list:
            yield xs


class DBMongo(object):

    def __init__(self, name):
        self.client = MongoClient("121.41.206.74")
        self.db = self.client["newrank"][name]

    def __del__(self):
        self.client.close()


def get_type():
    _url = "https://www.newrank.cn/public/info/list.html"  # ?period=xinshi_day&type=data

    req = requests.get(_url)
    req.encoding = "utf-8"
    html = etree.HTML(req.text)
    type_list = html.xpath('//div[@class="xinshi-right-type-list"]/span')
    for item in type_list:
        t_name = item.xpath("./text()")
        yield t_name[0]


def main():
    for item in get_type():
        db_name = "xinshi_" + item
        print(db_name)
        mongo = DBMongo(db_name)

        for data in xin_shi_get_data(item):
            mongo.db.insert_one(data)
            print(f"插入数据: [newrank][{db_name}: {data}]")

        mongo.__del__()



if __name__ == '__main__':
    main()


